#define DEBUG
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=5, maxm=200000;

int solve(int a, int b, int c, int d, int n, int m, int map[][maxm+1]) {
  const int fx[3][2]={{-1, 0}, {0, 1}, {1, 0}};
  static int que[maxn*maxm+1][2], dis[maxn+1][maxm+1];
  static bool book[maxn+1][maxm+1];
  memset(dis, 127, sizeof dis);
  que[1][0] = a, que[1][1] = b;
  dis[a][b] = 0;
  book[a][b] = true;
  for (int h=0, t=1; h<t; ) {
    h++;
    int x=que[h][0], y=que[h][1];
    book[x][y] = false;
    for (int i=0; i<3; i++) {
      int nx=x+fx[i][0], ny=y+fx[i][1];
      if (nx>0 && ny>0 && nx<=n && ny<=m && map[nx][ny] && dis[nx][ny]>dis[x][y]+1) {
	dis[nx][ny] = dis[x][y]+1;
	if (!book[nx][ny]) {
	  t++;
	  que[t][0] = nx, que[t][1] = ny;
	}
      }
    }
  }
  return dis[c][d]<=n*m ? dis[c][d] : -1;
}

int main() {
  freopen("maze.in", "r", stdin);
  freopen("maze.out", "w", stdout);

  static int map[maxn+1][maxm+1];
  int n, m, q;
  scanf("%d %d %d", &n, &m, &q);
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      scanf("%d", &map[i][j]);
    }
  }

  for (int i=1; i<=q; i++) {
    int opt;
    scanf("%d", &opt);
    if (opt==1) {
      int a, b;
      scanf("%d %d", &a, &b);
      map[a][b] = !map[a][b];
    } else {
      int a, b, c, d;
      scanf("%d %d %d %d", &a, &b, &c, &d);
      printf("%d\n", solve(a, b, c, d, n, m, map));
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
